<?php
namespace app\admin\controller;

use app\admin\tool\Parser;
use app\common\model\Purchase as PurchaseModel;
use app\common\model\Styles as StylesModel;
use app\common\model\Brands as BrandsModel;
use think\Request;

class Purchase extends Base{

    public function index(){

        $this->data['title'] = '团购列表';
        $this->data['column'] = $this->column();
        $this->data['parser'] = new Parser();
        $this->data['model'] = PurchaseModel::where([])->order('add_time')->paginate(10);
        return view('',$this->data);
    }

    public function add(){
        $this->data['title'] = '添加团购';
        return view('',$this->data);
    }

    public function edit(Request $request){
        $id = $request->param('id');
        $model = PurchaseModel::get($id);



        //前端使用
        $exists_styles = [];
        $exists_brand = $model['related'];
        $styles = [];
        $brands  = [];

        $model['related'] = json_decode($model['related'],true);
        foreach($model['related'] as $key => $val ){
            array_push($styles,$key);
            if(!empty($val)){
                foreach($val as $kk => $vv ){
                    array_push($brands,$kk);
                }
            }
        }



        foreach($model['related'] as $key => $val ){
            $exists_styles[$key] = $key;
        }
        $exists_styles = json_encode($exists_styles);


        //var_dump($model['related']);exit;
        $styles = StylesModel::where('oid','in',$styles)->column('oid,name','oid');
        $brands = BrandsModel::where('oid','in',$brands)->column('oid,name','oid');


        $this->data['title'] = '编辑团购';
        $this->data['model'] = $model;
        $this->data['styles'] = $styles;
        $this->data['brands'] = $brands;
        $this->data['exists_brand'] = $exists_brand;
        $this->data['exists_styles'] = $exists_styles;

        return view('',$this->data);
    }


    public function create(Request $request){
        $data = $request->post();
        /**
            title: 六月新活动
            type: 1
            time_ranger: 2018-2019
            style[45][1]: 1
            style[45][2]: 2
            style[45][3]: 3
            style[46][8]: 8
            style[46][9]: 9
            style[46][11]: 11
         */
        if(empty($data['style'])){$this->error("请选择风格和品牌");}
        if(empty($data['title'])){$this->error("请填写标题");}
        if(empty($data['time_ranger'])){$this->error("请填写起止时间");}
        $timeRanger = explode("-",$data['time_ranger']);
        $startTime = strtotime($timeRanger[0]);
        $endTime = strtotime($timeRanger[1]);

        $insertData = [];
        $insertData['start_time'] = $startTime;
        $insertData['end_time'] = $endTime;
        $insertData['type'] = $data['type'];
        $insertData['title'] = $data['title'];
        $insertData['related'] = [];

        foreach($data['style'] as $item){
            if(!empty($data['brand'][$item])){
                $insertData['related'][$item] = $data['brand'][$item];
            }else{
                $insertData['related'][$item] = [];
            }
        }
        $insertData['related'] = json_encode($insertData['related']);
        $insertData['add_time'] = $insertData['update_time'] = time();
        $insertData['op_user'] = $this->admin->id;
        $res = PurchaseModel::create($insertData);
        if($res){
            $this->success("添加成功");
        }
        $this->error("添加失败");
    }


    public function update(Request $request){
        $data = $request->post();

        if(empty($data['style'])){$this->error("请选择风格和品牌");}
        if(empty($data['title'])){$this->error("请填写标题");}
        if(empty($data['time_ranger'])){$this->error("请填写起止时间");}
        $timeRanger = explode("-",$data['time_ranger']);
        $startTime = strtotime($timeRanger[0]);
        $endTime = strtotime($timeRanger[1]);

        $insertData = [];
        $insertData['start_time'] = $startTime;
        $insertData['end_time'] = $endTime;
        $insertData['type'] = $data['type'];
        $insertData['title'] = $data['title'];
        $insertData['related'] = [];

        foreach($data['style'] as $item){
            if(!empty($data['brand'][$item])){
                $insertData['related'][$item] = $data['brand'][$item];
            }else{
                $insertData['related'][$item] = [];
            }
        }
        $insertData['related'] = json_encode($insertData['related']);
        $insertData['add_time'] = $insertData['update_time'] = time();
        $insertData['op_user'] = $this->admin->id;
        $purchaseModel = new PurchaseModel();
        $res = $purchaseModel->save($insertData,['id'=>$data['id']]);
        if($res){
            $this->success("更新成功");
        }
        $this->error("更新失败");
    }

    /**
     * 后期要加上删除检查 看看活动的情况
     * @param Request $request
     */
    public function delete(Request $request){
        $id = $request->param('id');
        PurchaseModel::where(['id'=>$id])->delete();
        $this->success("删除成功");
    }


    protected function column(){
        return [
            'title'      =>['name'=>'团购名称','fun'=>'text','options'=>[]],
            'type'       =>['name'=>'类型','fun'=>'translate','options'=>['map'=>['0'=>'自营活动','1'=>'工厂活动']]],
            'start_time' =>['name'=>'开始时间','fun'=>'datetime','options'=>[]],
            'end_time'   =>['name'=>'结束时间','fun'=>'datetime','options'=>[]],
            'update_time'=>['name'=>'更新时间','fun'=>'text','options'=>[]],
        ];
    }
}